﻿using System.Collections.Generic;
using System.Linq;

namespace cludg.Domain.Extensions
{
	public class DomainExtensionRegistry
	{
		protected readonly List<DomainExtension> Extensions = new List<DomainExtension>();
		protected readonly List<IDomainExtensionProvider> Providers = new List<IDomainExtensionProvider>();

		public DomainExtensionRegistry(IEnumerable<IDomainExtensionProvider> providers)
		{
			Providers.AddRange(providers);
		}

		public void Register(DomainExtension extension)
		{
			Extensions.Add(extension);
		}

		public IEnumerable<DomainExtension> Get<TDomain>() where TDomain : class
		{
			return Extensions.ForDomain<TDomain>().Concat(
					Providers.SelectMany(provider => 
						provider.GetExtensions().ForDomain<TDomain>()));
		}
	}
}
